// TOWN SCRIPT
//    Town 1: Vikida

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

begintownscript;

variables;

short i,c,choice;
short lead_char;

body;

beginstate INIT_STATE;
// This state called whenever this town is entered.
//Names
	set_name(6,"Arivan");
	set_char_dialogue_pic(6,1953,0);
	set_name(7,"Klinger");
	set_char_dialogue_pic(7,1924,0);
 set_name(37,"Sarisi");
 set_name(38,"Endora");
 set_name(19,"Barron");
 set_name(21,"Lairre");
 set_name(18,"Purdune");
 set_name(49,"Nablar");

// Regenerate NPCs
// Klinger ID = 101
// Valzier ID = 300
spawn_creature(51);
spawn_creature(52);
if (get_sdf(110,0) == 10) {
 add_char_to_party(51);
 set_flag(110,0,1);
 print_str_color("Klinger is able to rest and rejoins the party!",3);
}
if (get_sdf(110,1) == 10) {
 add_char_to_party(52);
 set_flag(110,1,1);
 print_str_color("Valzier is able to rest and rejoins the party!",3);
}
erase_char(51);
erase_char(52);

c = 0;

 i = 0;
 while (i < 5) {
  if (char_ok(i)) {
   lead_char = i;
   i = 5;
  }
  else
   i = i + 1;
 }

 if (get_sdf(1,29) != 0)
  make_town_hostile();

 if ((get_sdf(0,7) == 0) && (get_sdf(1,29) == 0))
  activate_hidden_group(1);

 if (get_sdf(110,0) >= 1)
  erase_char(7);
	
	set_crime_tolerance(1);

break;

beginstate EXIT_STATE;
// Always called when the town is left.
break;

beginstate START_STATE;

 if ((get_ran(1,0,100) < 7) && (get_attitude(6) == 3) && (get_sdf(0,7) > 3))
		text_bubble_on_char(6,"I rule this forest!");
 if ((get_ran(1,0,100) < 7) && (get_attitude(6) == 3) && (get_sdf(0,7) > 3))
		text_bubble_on_char(6,"Leave immediately.");

 if ((get_ran(1,0,100) < 7) && (get_attitude(6) == 3))
		text_bubble_on_char(49,"Come here, outsiders.");
	if ((get_ran(1,0,100) < 7) && (get_attitude(6) == 3))
		text_bubble_on_char(49,"We should speak.");

 if ((get_ran(1,0,100) < 7) && (get_attitude(38) == 3))
		text_bubble_on_char(38,"Oh dear.");
	if ((get_ran(1,0,100) < 7) && (get_attitude(38) == 3))
		text_bubble_on_char(38,"What will I do?");

 if ((get_ran(1,0,100) < 7) && (get_attitude(18) == 3))
		text_bubble_on_char(18,"Whew.");
	if ((get_ran(1,0,100) < 7) && (get_attitude(18) == 3))
		text_bubble_on_char(18,"Finest swords here!");

 if ((get_ran(1,0,100) < 7) && (get_attitude(21) == 3))
		text_bubble_on_char(21,"Ho, hum.");
	if ((get_ran(1,0,100) < 7) && (get_attitude(21) == 3))
		text_bubble_on_char(21,"Fresh food.");

 if ((get_ran(1,0,100) < 7) && (get_attitude(19) == 3))
		text_bubble_on_char(19,"Must work carefully.");
	if ((get_ran(1,0,100) < 7) && (get_attitude(19) == 3))
		text_bubble_on_char(19,"Only make quality products.");

 if ((get_ran(1,0,100) < 7) && (get_attitude(37) == 3))
		text_bubble_on_char(37,"Only the strong survive.");
	if ((get_ran(1,0,100) < 7) && (get_attitude(37) == 3))
		text_bubble_on_char(37,"Eliminate the weak.");

if ((get_sdf(0,7) == 1) && (c >= 1)) {
 set_flag(0,7,3);
 message_dialog("The guards take your weapons and lead you to a large wooden structure on the other side of the village. The building is quite impressive especially considering it was built by some isolated tribe of Nephilim.","You are led through the halls of the wooden fortress. You hope the leader of this tribe is as merciful as the guards said.");
 relocate_character(lead_char,41,30);
 set_character_facing(lead_char,0);
 if (lead_char < 3) {
  relocate_character(lead_char + 1,40,31);
  set_character_facing(lead_char + 1,0);
 }
 if (lead_char < 2) {
  relocate_character(lead_char + 2,41,31);
  set_character_facing(lead_char + 2,0);
 }
 if (lead_char < 1) {
  relocate_character(lead_char + 3,42,31);
 set_character_facing(lead_char + 3,0);
 }
 force_view_center(41,30);
 force_instant_terrain_redraw();

 erase_char(8);
 erase_char(9);
 erase_char(10);
 erase_char(11);
 erase_char(12);
 erase_char(13);
 erase_char(14);
 erase_char(15);

 begin_talk_mode(5);

}

if (get_sdf(0,7) == 1)
 c = c + 1;

if ((get_sdf(110,0) == 1) && (get_sdf(0,22) < 2))
 inc_flag(0,22,1);

if ((get_sdf(110,0) == 1) && (get_sdf(0,22) == 2)) {
 set_flag(0,22,3);
 message_dialog("_I suppose I really should tell you how I'm getting out of here. We cannot go out the way you came in. The guards will recognize me and kill us all. In fact, as long as I am with you, I would not enter this building._","_Our means of escape is in the northern cell. Let's go there and we can get out of here._");
 }
	
break;


beginstate 10;

reset_dialog();
if ((get_sdf(0,20) >= 1) && (get_terrain(43,24) != 21)) {
	add_dialog_str(0,"This must be the elk head Klinger told you about. You look at the left horn and notice a peculiar mechanical aspect to it. Pulling it should reveal a passage into Arivan's chambers.",0);
	add_dialog_choice(0,"Pull the horn.");
	add_dialog_choice(1,"Leave the lever alone.");
	choice = run_dialog(1);
	if (choice == 1) {
		set_terrain(43, 24, 21);
  play_sound(34);
  message_dialog("You pull the horn, and the wall next to it shimmers. You notice the magically induced outline of a secret door. A crude magical espionage technique, but an effective one.","");
 }
	if (choice == 2)
		end();
}

break;

beginstate 11;

if (get_attitude(6) == 3)  {
 block_entry(1);
 message_dialog("Two burly Nephilim guards protect this door and they refuse to let you pass. Whatever lies in the chamber beyond is clearly not for you.","");
 }

break;

beginstate 12;

if (get_attitude(6) == 3)  {
 block_entry(1);
 message_dialog("Now that you have snuck in Arivan's chambers, it would be unwise to take the main entrance out.","");
 }

break;

beginstate 13;

reset_dialog();
	add_dialog_str(0,"This appears to be Arivan's journal. It's a messy thing stuffed with lots of loose notes. You notice several green runes on the corner of the book. Looks like a crude magical ward. Reading the book would be risky.",0);
	add_dialog_choice(0,"Read it.");
	add_dialog_choice(1,"Leave the journal alone.");
	choice = run_dialog(1);
	if (choice == 1)
  set_state_continue(14);
	if (choice == 2)
		end();

break;

beginstate 14;

reset_dialog();
 add_dialog_str(0,"As you open the book, the runes fizzle slightly. You feel an odd sensation, but the feeling quickly passes. You wait and nothing visible happens. After several seconds, you decide to page through it.",0);
 add_dialog_str(1,"The journal details all of Arivan's ramblings on _achieving a perfect society_ and her _path to purity_. The details and beliefs seem quite warped. You wonder how these radical beliefs became valued in this tribe.",0);
 add_dialog_str(2,"As you page through, you read passages of how she _cleansed_ several small villages of those who were _inadequate for perfection_. From the notes, you can bet that is what happened to the village to the south.",0);
 add_dialog_str(3,"More recently, however, Arivan seems obsessed with the tribe of Asmur to the north. She seems intrigued by their _technological advancement_, but laments their _rejection of perfectionist ideals_.",0);
 add_dialog_str(4,"She seems them as a _threat to the achievement of true perfection_ and her own power and has concluded they must be _eliminated_.",0);
	add_dialog_choice(0,"Read on.");
 choice = run_dialog(1);

reset_dialog();
 add_dialog_str(0,"Over the past months, it seems, Arivan has been deploying sizable forces to the north in preparation for a _lightning strike force_ that will _utterly eliminate_ the threat to her power.",0);
 add_dialog_str(1,"Of the many notes you find crammed in the journal, one reads, _I have infiltrated Asmur. I hope I am now worthy._ It is unsigned. So, Arivan has a spy. You take the note. You doubt she will notice with so much clutter.",0);
 add_dialog_str(2,"The final entry details that the plan is almost ready to be carried out. She will strike _at the most opportune moment_ that will be _very soon_. However, she does not go into specifics.",0);
 add_dialog_str(3,"Klinger would definitely be very interested in this. You try to remember as many details as possible before closing the journal.",0);
	add_dialog_choice(0,"OK.");
 choice = run_dialog(1);


if (get_sdf(0,21) == 0)
 set_flag(0,21,1);

break;

beginstate 15;

if ((get_sdf(110,0) == 1) && (get_attitude(6) == 3))
 message_dialog("_Um, I really don't think this is the way we want to go. If the guards see me out of my cell, they will kill us for sure. I have an escape in the northern cell, let's go there.","");

break;

beginstate 16;

if ((get_sdf(110,0) == 1) && (get_sdf(0,23) == 0)) {

 set_flag(0,23,1);

 relocate_character(4,52,27);
 set_character_facing(4,0);
 force_instant_terrain_redraw();
 pause(3);

 text_bubble_on_char(4,"Let's see...");
 force_instant_terrain_redraw();
 pause(5);

 text_bubble_on_char(4,"");
 text_bubble_on_char(4,"I believe it's right here.");
 force_instant_terrain_redraw();
 pause(2);

 play_sound(94);
 set_terrain(52,27,0);
 force_instant_terrain_redraw();
 pause(3);

 text_bubble_on_char(4,"");
 text_bubble_on_char(4,"There it is.");
 force_instant_terrain_redraw();
 pause(5);

 text_bubble_on_char(4,"");
 relocate_character(4,52,26);
 force_instant_terrain_redraw();
 pause(3);

 set_character_facing(4,4);
 text_bubble_on_char(4,"Come on!");
 force_instant_terrain_redraw();
 pause(3);

 text_bubble_on_char(4,"");
 force_instant_terrain_redraw();

}

break;

beginstate 17;

if ((get_sdf(0,24) == 0) && (get_terrain(57,27) == 0) && (get_sdf(110,0) == 1)) {
 set_flag(0,24,1);
 message_dialog("_Okay, now we're out of there. The best way to escape the city is through a secret tunnel in one of the storerooms on the northern part of the village. I believe it is the westernmost storeroom._","_From there we should be able to reach Asmur without having to contend with the Vikida guards. We need to keep a low profile from now on and should definitely not return to this building. Let's go!_");
}

break;


beginstate 18;

if ((get_sdf(110,0) == 1) && (get_attitude(6) == 3)) {
 make_town_hostile();
 message_dialog("The guards recognize Klinger as an escaped prisoner! Seeing you with him does not help your status. They shout out an alarm and draw their weapons. This place is no longer friendly to you.","");
 }

break;


beginstate 19;

if ((get_sdf(110,0) == 1) && (get_sdf(0,25) == 0)) {
 set_flag(0,25,1);

 relocate_character(4,23,10);
 set_character_facing(4,0);
 force_instant_terrain_redraw();
 pause(3);

 relocate_character(4,23,9);
 force_instant_terrain_redraw();
 pause(3);

 relocate_character(4,23,8);
 force_instant_terrain_redraw();
 pause(3);

 text_bubble_on_char(4,"Hm...");
 force_instant_terrain_redraw();
 pause(5);

 text_bubble_on_char(4,"");
 relocate_character(4,22,8);
 set_character_facing(4,2);
 force_instant_terrain_redraw();
 pause(3);

 set_character_facing(4,0);
 force_instant_terrain_redraw();
 pause(3);

 play_sound(9);
 pause(5);

 relocate_character(4,23,8);
 set_character_facing(4,6);
 force_instant_terrain_redraw();
 pause(3);

 relocate_character(4,24,8);
 force_instant_terrain_redraw();
 pause(3);

 set_character_facing(4,0);
 force_instant_terrain_redraw();
 pause(3);

 play_sound(9);
 pause(5);

 relocate_character(4,23,8);
 set_character_facing(4,2);
 force_instant_terrain_redraw();
 pause(3);

 set_character_facing(4,0);
 force_instant_terrain_redraw();
 pause(3);

 play_sound(45);
 pause(2);
 play_sound(45);
 pause(2);
 play_sound(45);
 pause(2);
 set_terrain(23,7,0);
 force_instant_terrain_redraw();
 pause(5);

 text_bubble_on_char(4,"This way.");
 set_character_facing(4,4);
 force_instant_terrain_redraw();
 pause(5);

 text_bubble_on_char(4,"");
 relocate_character(4,23,7);
 set_character_facing(4,0);
 force_instant_terrain_redraw();
 pause(3);

 relocate_character(4,23,6);
 force_instant_terrain_redraw();
 pause(3);

 relocate_character(4,6,6);
 force_instant_terrain_redraw();
 pause(3);

}

break;


beginstate 20;

reset_dialog();
if ((get_sdf(0,25) >= 1) && (get_terrain(23,7) != 0)) {
	add_dialog_str(0,"The face of this cliff hides a secret passage leading outside into the city. By pulling the horns on the elk heads, you can reveal it.",0);
	add_dialog_choice(0,"Reveal the passage.");
	add_dialog_choice(1,"Leave.");
	choice = run_dialog(1);
	if (choice == 1) {
  set_terrain(23,7,0);
  play_sound(45);
  message_dialog("You pull the horns in the proper order and the stone face of the cliff slides away revealing a tunnel.","");
 }
	if (choice == 2)
		end();
}

break;


beginstate 21;

change_outdoor_location(0,0,36,41);
move_to_new_town(2,24,38);

break;


beginstate 22;

if ((get_sdf(2,3) == 0) && (get_attitude(6) == 3)) {
 set_flag(2,3,1);
 message_dialog("This room is full of the sounds of joyous laughter and carries the atmosphere of a seedy tavern. Although the inhabitants are Nephilim and you do not recognize the games, the same desires remain constant for both races.","You notice an individual at the table by himself eyeing you curiously.");
}

break;

beginstate 23;

if ((get_sdf(2,7) == 0) && (get_attitude(6) == 3)) {
 set_flag(2,7,1);
 message_dialog("You walk outside of the main hall into the streets of Vikida. All of the residents look at you nervously and turn away. Everyone seems to want to not notice you, but cannot help it.","You sense an odd tension and fear in this place.");
}

break;